package com.rabboit.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: LI-CI-GAO
 * 作者: licigao
 * 日期: 2021/8/31  20:34
 * 描述:
 */
@Configuration
public class TEstQueueConfig {

    @Bean("queue1")
    public Queue queue1(){
       return new Queue("TEST-123-QUEUE",true,false,false);
    }

    @Bean("directExchange")
    public Exchange getDirectExchange(){
        Exchange build = ExchangeBuilder.directExchange("TEST-123-EXCHANGE").durable(true).build();
        return build;
    }

    @Bean("binding1")
    public Binding getBinding(@Qualifier("queue1") Queue queue1, @Qualifier("directExchange") Exchange directExchange){
        Binding noargs = BindingBuilder.bind(queue1).to(directExchange).with("KEY-123").noargs();
        return noargs;
    }


    ///////////////////广播交换机
    /*@Bean("queue2")
    public Queue queue2(){
        return new Queue("TEST-QUEUE2",true,false,false);
    }

    @Bean("queue3")
    public Queue queue3(){
        return new Queue("TEST-QUEUE3",true,false,false);
    }

    @Bean("fandoutExchange")
    public Exchange getFandoutExchange(){
        Exchange build = ExchangeBuilder.fanoutExchange("FANOUT-EX").durable(true).build();
        return build;
    }

    @Bean("binding2")
    public Binding getBinding2(@Qualifier("queue2") Queue queue2, @Qualifier("fandoutExchange") Exchange directExchange){
        Binding noargs = BindingBuilder.bind(queue2).to(directExchange).with("").noargs();
        return noargs;
    }

    @Bean("binding3")
    public Binding getBinding3(@Qualifier("queue3") Queue queue3, @Qualifier("fandoutExchange") Exchange directExchange){
        Binding noargs = BindingBuilder.bind(queue3).to(directExchange).with("").noargs();
        return noargs;
    }*/


    //topic====================================================
    /*@Bean("queue4")
    public Queue queue4(){
        return new Queue("TEST-QUEUE4",true,false,false);
    }

    @Bean("queue5")
    public Queue queue5(){
        return new Queue("TEST-QUEUE5",true,false,false);
    }

    @Bean("topicExchange")
    public Exchange getTopicExchange(){
        Exchange build = ExchangeBuilder.topicExchange("TOPIC-EX").durable(true).build();
        return build;
    }

    @Bean("binding4")  //
    public Binding getBinding4(@Qualifier("queue4") Queue queue4, @Qualifier("topicExchange") Exchange topicExchange){
        Binding noargs = BindingBuilder.bind(queue4).to(topicExchange).with("*.A.*").noargs();
        return noargs;
    }
    @Bean("binding5")  //
    public Binding getBinding5(@Qualifier("queue5") Queue queue5, @Qualifier("topicExchange") Exchange topicExchange){
        Binding noargs = BindingBuilder.bind(queue5).to(topicExchange).with("#.A.#").noargs();
        return noargs;
    }*/


    //=======================死信队列
    @Bean("queue6")
    public Queue queue6(){
        return new Queue("TEST-QUEUE6",true,false,false);
    }

    @Bean("directExchange6")
    public Exchange getDirectExchange6(){
        Exchange build = ExchangeBuilder.directExchange("ZHI-LIAN6").durable(true).build();
        return build;
    }

    @Bean("binding6")  //
    public Binding getBinding6(@Qualifier("queue6") Queue queue6, @Qualifier("directExchange6") Exchange directExchange6){
        Binding noargs = BindingBuilder.bind(queue6).to(directExchange6).with("dead-key").noargs();
        return noargs;
    }

    //=======================死信队列 end

    @Bean("queue7")
    public Queue queue7(){
        Map<String, Object> arguments=new HashMap<>();
        arguments.put("x-dead-letter-exchange","ZHI-LIAN6");
        arguments.put("x-dead-letter-routing-key","dead-key");
        return new Queue("TEST-QUEUE7",true,false,false,arguments);
    }

    @Bean("directExchange7")
    public Exchange getDirectExchange7(){
        Exchange build = ExchangeBuilder.directExchange("ZHI-LIAN7").durable(true).build();
        return build;
    }


    @Bean("binding7")  //
    public Binding getBinding7(@Qualifier("queue7") Queue queue7, @Qualifier("directExchange7") Exchange directExchange7){
        Binding noargs = BindingBuilder.bind(queue7).to(directExchange7).with("ZHENG-CHANG").noargs();
        return noargs;
    }




    @Bean("directExchange10")
    public Exchange getDirectExchange10(){
        Exchange build = ExchangeBuilder.directExchange("ZHI-LIAN10").durable(true).build();
        return build;
    }


    /*******************延时消息的使用********************/
    @Bean("yanShiQueue")
    public Queue delayQueue(){
        return new Queue("YAN-SHI-QUEUE");
    }


    @Bean("yanShiExchange")
    public Exchange getYanShiExchange(){
        Map<String, Object> arguments=new HashMap<>();
        arguments.put("x-delayed-type","direct");
        return new CustomExchange("YAN-SHI-EXCHANGE","x-delayed-message",true,false,arguments);
    }


    @Bean("binding8")
    public Binding getBinding8(@Qualifier("yanShiQueue") Queue queue, @Qualifier("yanShiExchange") Exchange directExchange){
        Binding noargs = BindingBuilder.bind(queue).to(directExchange).with("KEY-123").noargs();
        return noargs;
    }

}
